import {Route,Routes,Navigate,HashRouter} from 'react-router-dom'
import { lazy,Suspense } from 'react'

let arr=[
    {
        path:'/',
        to:'/home'
    },
    {
        path:'/home',
        element:lazy(()=>import('../view/Home/index')),
        children:[
            {
                path:'user/list',
                element:lazy(()=>import('../view/User/list'))
            },
             {
                path:'role/list',
                element:lazy(()=>import('../view/Role/list'))
            },
            {
                path:'role/add',
                element:lazy(()=>import('../view/Role/add'))
            },
            {
                path:'role/edit/:id',
                element:lazy(()=>import('../view/Role/edit'))
            }
        ]
    },
    {
        path:'/login',
        element:lazy(()=>import('../view/Login/index'))
    }
]

const Router=()=>{
    return (
        <Suspense fallback={<div>loading</div>}>
            <HashRouter>
                <Routes>
                    {
                        arr.map((item,index)=>{
                            return (
                                <Route key={index} path={item.path} element={item.element?<item.element></item.element>:<Navigate to={item.to}></Navigate>}>
                                    {/* 判断是否有子路由，且子路由的数组长度不为0 */}
                                    {
                                        item.children && item.children.length?item.children.map((v,i)=>{
                                            return (
                                                <Route key={i} path={v.path} element={v.element?<v.element></v.element>:<Navigate to={v.to}></Navigate>}>

                                                </Route>
                                            )
                                        }):''
                                    }
                                </Route>
                            )
                        })
                    }
                </Routes>
            </HashRouter>
        </Suspense>
    )
}

export default Router;